package ru.spb.osll.myfit.common;

import java.util.Comparator;

public class ExecutingResultComparatorFactory {

  public enum ComparatorType {BY_NAME, BY_TIME, BY_STATE};

  public static Comparator<ExecutingResult> createComparator(ComparatorType type){
    switch(type){
      case BY_TIME:
        return new TimeComparator();
      case BY_STATE:
        return new StateComparator();
      case BY_NAME:
      default:
        return new NameComparator();
    }
  }

  private static class NameComparator implements Comparator<ExecutingResult> {
    public int compare(ExecutingResult o1, ExecutingResult o2) {
      if (o1 == o2)
        return 0;

      if (o1 != null)
        return (o2 != null) ? o1.getName().compareTo(o2.getName()) : 1;

      return -1;
    }
  }

  private static class TimeComparator implements Comparator<ExecutingResult> {
    public int compare(ExecutingResult o1, ExecutingResult o2) {
      if (o1 == o2)
        return 0;

      if (o1 != null)
        return (o2 != null) ? o1.getStartTime().compareTo(o2.getStartTime()) : 1;

      return -1;
    }
  }


  private static class StateComparator implements Comparator<ExecutingResult> {
    public int compare(ExecutingResult o1, ExecutingResult o2) {
      if (o1 == o2)
        return 0;

      if (o1 != null)
        return (o2 != null) ? o1.getState().compareTo(o2.getState()) : 1;

      return -1;
    }
  }

}